import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/model/product.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/model/coupon_item.dart';

class CouponListItemWidget extends StatelessWidget {
  CouponItem couponItem;
  double width;
  Function onClickCoupon;
  Function onReceiveCoupon;

  CouponListItemWidget(
      this.couponItem, this.width, this.onClickCoupon, this.onReceiveCoupon);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onClickCoupon,
      child: Container(
        width: width,
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(width: 1.0, color: ColorCommon.black4)),
        child: Stack(
          children: <Widget>[
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                CachedNetworkImage(
                  height: width,
                  width: width,
                  imageUrl: couponItem.spu != null &&
                          couponItem.spu != null &&
                          couponItem.spu.avatar != null
                      ? couponItem.spu.avatar
                      : Api.defaultAvatar,
                  fit: BoxFit.fill,
                ),
                Expanded(
                  child: Container(
                    padding: const EdgeInsets.only(
                        top: 4.0, bottom: 4.0, left: 4.0, right: 4.0),
                    child: Text(
                      couponItem?.spu?.name ?? "",
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                      style:
                          TextStyle(fontSize: 12.0, color: ColorCommon.black1),
                    ),
                  ),
                ),
                Container(
                  padding: const EdgeInsets.only(left: 4.0, right: 4.0),
                  child: Row(children: <Widget>[
                    Expanded(
                      child: Text(
                        "现价¥" +
                            (couponItem?.spu?.lowSalePrice != null
                                ? couponItem?.spu?.lowSalePrice.toString()
                                : "¥0.0"),
                        style:
                            TextStyle(color: ColorCommon.grey8, fontSize: 10.0),
                      ),
                    ),
                    Text(
                      "已售出0件",
                      style:
                          TextStyle(color: ColorCommon.grey8, fontSize: 10.0),
                    ),
                  ]),
                ),
                Container(
                  margin: const EdgeInsets.only(
                      top: 4.0, bottom: 4.0, left: 8.0, right: 8.0),
                  height: 1.0,
                  color: ColorCommon.grey5,
                ),
                Container(
                  padding: const EdgeInsets.only(
                      top: 4.0, bottom: 6.0, left: 4.0, right: 4.0),
                  child: Row(children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 2.0, right: 2.0),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: ColorCommon.primaryColor,
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(2.0),
                            bottomLeft: Radius.circular(2.0),
                          ),
                          border:
                              Border.all(width: 1.0, color: ColorCommon.primaryColor)),
                      child: Text(
                        "券",
                        style: TextStyle(fontSize: 12.0, color: Colors.white),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(left: 2.0, right: 2.0),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(2.0),
                            bottomRight: Radius.circular(2.0),
                          ),
                          border:
                              Border.all(width: 1.0, color: ColorCommon.primaryColor)),
                      child: Text(
                        (couponItem.coupon.money != null
                                ? couponItem.coupon.money.toString()
                                : "0") +
                            "元",
                        style:
                            TextStyle(fontSize: 12.0, color: ColorCommon.primaryColor),
                      ),
                    ),
                    Expanded(
                      child: Container(),
                    ),
                    InkWell(
                      onTap: onReceiveCoupon,
                      child: _getTagView(
                          // couponItem.had != null && couponItem.had
                          //     ? "已领取"
                          //     : "领券",
                          "领券",
                          ColorCommon.primaryColor,
                          ColorCommon.primaryColor,
                          Colors.white),
                    ),
                  ]),
                ),
              ],
            ),
            Positioned(
              left: 0.0,
              top: 0.0,
              child: Container(
                width: width / 3,
                height: width / 3,
                decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("images/coupon_tag.png"),
                      fit: BoxFit.fill),
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      "优惠券",
                      style: TextStyle(color: Colors.white, fontSize: 12.0),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(bottom: 5.0),
                      child: Text(
                        "¥${(couponItem.coupon.money != null ? couponItem.coupon.money.toString() : "0")}",
                        style: TextStyle(color: Colors.white),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getTagView(
      String text, Color bgColor, Color borderColor, Color textColor,
      {DecorationImage image}) {
    BoxDecoration decoration;
    if (image == null) {
      decoration = BoxDecoration(
          color: bgColor,
          border: Border.all(color: borderColor),
          borderRadius: BorderRadius.all(Radius.circular(100.0)));
    } else {
      decoration = BoxDecoration(image: image);
    }
    return Container(
      padding: const EdgeInsets.only(left: 15.0, right: 15.0),
      decoration: decoration,
      child: Text(
        text,
        style: TextStyle(fontSize: 12.0, color: textColor),
      ),
    );
  }
}
